<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) return;
	
	$riterator = $GLOBALS['DATABASE']->get('rooms');
	$rooms = array();
	while($rdata = $riterator->get_next()){
		$room = new lib_room($rdata['rid']);
		$rid = $rdata['rid'];
		if(!$room->is_valid()) continue;

		// Lookup people in the room
		$online_users = $room->get_online_users();
		$online_count = count($online_users);

		// By default assume user has access to room
		$access = true;
		
		// Check to see if room has a password
		if($room->local_permission_value(null,'PASSWORD_CORRECT') == 1){
			$pass = false;
		}elseif(!empty($room->data_cache['password'])){
			$pass = true;
		}else{
			$pass = false;
		}
		
		// Check if access is denied by access list
		if(!$pass && !$room->has_permission($ses->user,'PASSWORD_CORRECT'))
			$access = false;
			
		// Check if access is denied because room is full
		elseif($online_count >= $room->data_cache['max_users'] && !$room->has_permission($ses->user,'ENTER_FULL_ROOM'))
			$access = false;
			
		// Check if access is denied because of a ban
		elseif(!$room->has_permission($ses->user,'ACCESS_ROOM') && !$room->check_ban_expiration($ses->user->data_cache['uid']) && !$ses->user->has_permission('BAN_IMMUNITY'))
			$access = false;

		if($access)
			$rooms[] = array('rdata'=>$room->data_cache,'ppl'=>$online_count,'pass_required'=>$pass,'access_allowed'=>$access);
	
	}
	
	echo tpl_roomlist::roomlist($rooms,$ses->user->has_permission('ACP_TOOLS'));
	return;
?>